#include "./stack.h"
#include <stdlib.h>

Stack *initStack()
{
	Stack *stack = (Stack *)malloc(sizeof(Stack));
	stack->base = (char *)calloc(MAX_SIZE, sizeof(char));
	stack->top = stack->base;
	stack->stackMaxSize = MAX_SIZE;
	return stack;
}

int stackEmpty(Stack *stack)
{
	return stack->base == stack->top;
}

int stackLength(Stack *stack)
{
	return stack->top - stack->base;
}

char getTop(Stack *stack)
{
	if (stackEmpty(stack))
	{
		return 0;
	}
	return *(stack->top - 1);
}

void push(Stack *stack, char e)
{
	// 如果栈满了就再追加10个元素的空间
	if (stack->top - stack->base >= stack->stackMaxSize)
	{
		stack->base = (char *)realloc(stack->base, (stack->stackMaxSize + 10) * sizeof(char));
		// 重置顶指针
		stack->top = stack->base + stack->stackMaxSize;
		stack->stackMaxSize += 10;
	}
	*stack->top = e;
	stack->top++;
}

char pop(Stack *stack)
{
	if (stackEmpty(stack))
	{
		return 0;
	}
	char e = *(stack->top - 1);
	stack->top--;
	return e;
}